package com.cameron.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.cameron.entity.BackendMemberDto;
import io.jsonwebtoken.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author not me
 * @since 2019/10/16
 */
public class JwtUtils {

    //token 过期时间
    public static final long EXPIRE = 1000 * 60 * 60 * 24;
    //加密 密钥
    public static final String APP_SECRET = "dGltaG9vcGVyIGZ1Y2sgbWUgcGxz";

    public static String getJwtToken(String id, String nickname) {
        String JwtToken = Jwts.builder()
                //设置头信息
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                //设置分类
                .setSubject("guli-user")
                //设置过期时间
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                //设置token主体部分
                .claim("id", id)
                .claim("nickname", nickname)
                //设置加密 密钥
                .signWith(SignatureAlgorithm.HS256, APP_SECRET)
                .compact();
        return JwtToken;
    }

    public static String getJwtToken(String id, String nickname, int roles) {
        String JwtToken = Jwts.builder()
                //设置头信息
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                //设置分类
                .setSubject("guli-user")
                //设置过期时间
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                //设置token主体部分
                .claim("id", id)
                .claim("nickname", nickname)
                .claim("roles", roles)
                //设置加密 密钥
                .signWith(SignatureAlgorithm.HS256, APP_SECRET)
                .compact();
        return JwtToken;
    }

    public static BackendMemberDto getLoginMemberByToken(String token) {
        if ((StringUtils.isEmpty(token))) {
            return null;
        }
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(token);

        //获取主体
        Claims claims = claimsJws.getBody();
        String id = (String) claims.get("id");
        String name = (String) claims.get("nickname");
        int roles = (int) claims.get("roles");
        BackendMemberDto memberDto = new BackendMemberDto();
        memberDto.setId(id);
        memberDto.setName(name);
        memberDto.setRoles(roles);
        return memberDto;
    }

    /**
     * 判断token是否存在与有效
     *
     * @param jwtToken
     * @return
     */
    public static boolean checkToken(String jwtToken) {
        if (StringUtils.isEmpty(jwtToken)) {
            return false;
        }
        try {
            Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 判断token是否存在与有效
     *
     * @param request
     * @return
     */
    public static boolean checkToken(HttpServletRequest request) {
        String jwtToken = request.getHeader("token");
        return checkToken(jwtToken);
    }

    /**
     * 根据token获取会员id
     *
     * @param request
     * @return
     */
    public static String getMemberIdByJwtToken(HttpServletRequest request) {
        String jwtToken = request.getHeader("token");
        if ((StringUtils.isEmpty(jwtToken))) {
            return "";
        }
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);

        //获取主体
        Claims claims = claimsJws.getBody();

        //通过主体中的键获取对应的值
        return (String) claims.get("id");
    }
}
